package com.sinosoft.app.utils.kafka;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间处理工具类
 *
 * @author Administrator
 */
public class DateUtil {

    /**
     * 解析date
     *
     * @param date 18268L->date 1997-01-01
     * @return
     */
    public static LocalDate parseDate(long date) {
        return LocalDate.ofEpochDay(date);
    }

    /**
     * 解析Time
     *
     * @param date 12:00:00->47987000000L
     * @return
     */
    public static LocalTime parseTime(long date) {
        long sencond = date / 1000000;
        return LocalTime.ofSecondOfDay(sencond);
    }

    /**
     * 解析TIMESTAMP
     *
     * @param date 1997-01-01 00:00:00->1577884791000000L
     * @return
     */
    public static LocalDateTime parseTimeStamp(long date) {
        long sencond = date / 1000000;
        return LocalDateTime.ofEpochSecond(sencond, 0, ZoneOffset.UTC);
    }

    public static String parseInterval(long date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("GMT+:08:00"));
        return df.format(date / 1000);
    }

    /**
     * 解析数据库time类型
     * 不带时区
     *
     * @param time
     * @return
     */
    public static LocalTime parseTimeWithoutZoneStr(String time) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        return LocalTime.parse(time, formatter);
    }

    /**
     * 将long类型转化为date, 此处long类型需除1000再转化
     * @param time
     * @return
     */
    public static Date parseLongTime(long time){
        return new Date(time/1000);
    }

}
